package com.hsdev.caprace;

import java.io.IOException;

import org.andengine.engine.Engine;
import org.andengine.engine.LimitedFPSEngine;
import org.andengine.engine.camera.SmoothCamera;
import org.andengine.engine.camera.hud.HUD;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.WakeLockOptions;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.input.touch.detector.PinchZoomDetector;
import org.andengine.ui.activity.BaseGameActivity;

import com.badlogic.gdx.math.Vector2;
import com.hsdev.caprace.SceneManager.AllScenes;

public class MainActivity extends BaseGameActivity {
	
	
	private static final int WIDTH = 800;
	private static final int HEIGHT = 480;
	private SmoothCamera camera;
	private PinchZoomDetector mPinchZommDetector;
	private SceneManager sceneManager;
	private float initialZoomFactor = 1.5f;
	private float maxZoomFactor = 2.0f;
	private float minZoomFactor = 0.7f;
	private Engine engine;
	private PhysicsWorld physicsWorld = new PhysicsWorld(new Vector2(0, 0), false);
	
	@Override
	  public Engine onCreateEngine(EngineOptions pEngineOptions) {
	    engine = new LimitedFPSEngine(pEngineOptions, 60);
	    //engine = new FixedStepEngine(pEngineOptions, 60);
	    return engine;
	  }

	@Override
	public EngineOptions onCreateEngineOptions() {
		camera = new SmoothCamera(0, 0, WIDTH, HEIGHT, 500f, 500f, 0.5f);
		camera.setZoomFactor(initialZoomFactor);
		EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_SENSOR, new CropResolutionPolicy(WIDTH, HEIGHT), camera);
		engineOptions.setWakeLockOptions(WakeLockOptions.SCREEN_ON);
		return engineOptions;
	}

	@Override
	public void onCreateResources(
			OnCreateResourcesCallback pOnCreateResourcesCallback)
			throws IOException {
		
		sceneManager = new SceneManager(this, engine, camera);
		ResourceManager.getInstance().loadSplashTextures(engine, this);
		
		
		pOnCreateResourcesCallback.onCreateResourcesFinished();
		
	}

	@Override
	public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
			throws IOException {
		
		pOnCreateSceneCallback.onCreateSceneFinished(sceneManager.createSplashScene());
		
		
	}

	@Override
	public void onPopulateScene(Scene pScene,
			OnPopulateSceneCallback pOnPopulateSceneCallback)
			throws IOException {
		
		engine.registerUpdateHandler(new TimerHandler(3f, new ITimerCallback() {
			
			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				
				engine.unregisterUpdateHandler(pTimerHandler);
				ResourceManager.getInstance().loadGameTextures(engine, getBaseContext());
				sceneManager.createGameScene();
				sceneManager.setCurrentScene(AllScenes.GAME);
				
				
			}
		}));
		
		pOnPopulateSceneCallback.onPopulateSceneFinished();
		
	}

	
}
